/**
 * Created by Administrator on 2017/4/6.
 */
import {Injectable} from "@angular/core";
import {Http, Headers, Response} from "@angular/http";
import {User} from "./user";
import {Config} from "../config/config";
import {Observable} from "rxjs";
import "rxjs/add/operator/do";
import "rxjs/add/operator/map";

@Injectable()
export class Uservice {

    constructor(private _http: Http) {

    }

    login(user: User) {
        let creds = "username=" + user.username + "&password=" + user.password;
        let headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded');
        console.log(Config.loginUrl+"|" + creds );
        return this._http.post(Config.loginUrl, creds, {headers: headers, withCredentials: true})
            .catch(Uservice.handleErrors);
        // .subscribe(data => console.log(data),err => console.log(err),() => console.log('login Complete')).

    }

    static handleErrors(error: Response) {
        console.log(JSON.stringify(error.json()));
        return Observable.throw(error);
    }

    register() {
        alert("this is register")
    }

}